package com.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.somlsis.R;

import java.util.Timer;

public class LongClickCircleView extends View {
    private Context context;
    private Paint annulusPaint, progressPaint;   // 绘制画笔
    private RectF rectF;       // 绘制区域
    private int progressColor = Color.BLUE;  // 圆环进度色
    private int annulusColor = Color.LTGRAY;//圆环颜色
    private int progress;      // 圆环进度(0-100)
    private float annulusWidth;      // 圆环宽度
    private int viewWide, viewHigh;      // 圆环宽度
    private OnLongClickCircleViewListener listener;
    private Drawable src;

    public interface OnLongClickCircleViewListener {
        void onLongClickFinish();//长按结束
    }

    public LongClickCircleView(Context context) {
        super(context);
        this.context = context;
        init(null);
    }

    public LongClickCircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init(attrs);
    }

    public LongClickCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
    }

    public LongClickCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        float density = getResources().getDisplayMetrics().density;
        annulusWidth = 5 * density;

        TypedArray typedArray = attrs == null ? null : getContext().obtainStyledAttributes(attrs, R.styleable.LongClickCircleView);
        progressColor = typedArray.getColor(R.styleable.LongClickCircleView_progressColor, progressColor);
        annulusColor = typedArray.getColor(R.styleable.LongClickCircleView_annulusColor, annulusColor);
        annulusWidth = typedArray.getDimension(R.styleable.LongClickCircleView_annulusWidth, annulusWidth);
        src = typedArray.getDrawable(R.styleable.LongClickCircleView_src);
        // 初始化背景圆环画笔
        annulusPaint = new Paint();
        annulusPaint.setStyle(Paint.Style.STROKE);    // 只描边，不填充
        annulusPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        annulusPaint.setAntiAlias(true);              // 设置抗锯齿
        annulusPaint.setDither(true);                 // 设置抖动
        annulusPaint.setStrokeWidth(annulusWidth);
        annulusPaint.setColor(annulusColor);

        // 初始化进度圆环画笔
        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.STROKE);    // 只描边，不填充
        progressPaint.setStrokeCap(Paint.Cap.ROUND);   // 设置圆角
        progressPaint.setAntiAlias(true);              // 设置抗锯齿
        progressPaint.setDither(true);                 // 设置抖动
        progressPaint.setStrokeWidth(typedArray.getDimension(R.styleable.LongClickCircleView_annulusWidth, 5 * density));
        progressPaint.setColor(progressColor);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        viewWide = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        viewHigh = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        int mRectLength = (int) ((viewWide > viewHigh ? viewHigh : viewWide) - (annulusPaint.getStrokeWidth() > annulusPaint.getStrokeWidth() ? annulusPaint.getStrokeWidth() : annulusPaint.getStrokeWidth()));
        int mRectL = getPaddingLeft() + (viewWide - mRectLength) / 2;
        int mRectT = getPaddingTop() + (viewHigh - mRectLength) / 2;
        rectF = new RectF(mRectL, mRectT, mRectL + mRectLength, mRectT + mRectLength);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawArc(rectF, 0, 360, false, annulusPaint);
        canvas.drawArc(rectF, 275, 360 * progress / 100, false, progressPaint);
        if (src != null) {
            int srcWidth = viewWide / 3;
            int srcHeight = viewHigh / 3;
            int left = srcWidth;
            int top = srcWidth;
            int right = left + srcWidth;
            int bottom = top + srcHeight;
            src.setBounds(left, top, right, bottom);
            src.draw(canvas);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            progressHandler.sendEmptyMessage(action);
        } else if (action == MotionEvent.ACTION_UP)
            progressHandler.sendEmptyMessage(action);
        return true;
    }

    public void setOnLongClickCircleViewListener(OnLongClickCircleViewListener listener) {
        this.listener = listener;
    }

    private Handler progressHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            if (msg.what == MotionEvent.ACTION_DOWN) {
                if (progress == 100) {
                    removeCallbacksAndMessages(null);
                    if (listener != null)
                        listener.onLongClickFinish();
                    progress = 0;
                    invalidate();
                } else {
                    progress += 4;
                    invalidate();
                    sendEmptyMessageDelayed(msg.what, 50);
                }
            } else {
                removeCallbacksAndMessages(null);
                if (progress != 0) {
                    progress -= 4;
                    invalidate();
                    sendEmptyMessageDelayed(MotionEvent.ACTION_UP, 50);
                }
            }
        }
    };

    public void setSrc(int srcID) {
        src = context.getDrawable(srcID);
        invalidate();
    }

    public void setSrc(Drawable src) {
        this.src = src;
        invalidate();
    }
}
